$pathToEvExports = "\\chw-ev-003\e$\Exports\ArchiveReports"

## Function that imports the TSV files output by EV.
####################################################
function Import-TabDelimited ($Path) {
	$skiplines = 3  #Number of top lines to skip
	$filecheck = Get-Content $Path
	$totalLines = $filecheck.count
	$filecheck = $null
  Get-Content $path |ForEach-Object {$header = $true; $line = 1}  {
  	if ($line -le $skiplines) {}
    elseif ($header){
      $h = $_.split("`t")
      $header = $false
    }
    Else {
		$percent = ($line/ $totalLines) * 100
		Write-Progress -Activity "Importing report file" -Status "% Complete" -PercentComplete $percent -Id 1
      $r = new-object object
      $_.split("`t") |ForEach-Object {$i=0}{
        $r | add-Member -memberType noteProperty -name $h[$i] -value $_
        $i++
      }
      $r 
    }
	$line += 1
  }
}
###################################################

##############################
## Main                      # 
##############################


$ev = $Null
$ev = (1..4) | ForEach-Object { 
    Write-Progress -Activity "Loading EV Archive Data" -CurrentOperation "Importing EV Server $($_) data" -Status "% Complete" -PercentComplete ($_ * 25) -Id 0 
    Get-ChildItem "$($pathToEvExports)\EV_VN_EVMBAVS0$($_)*" | Sort-Object lastwritetime | Select-Object -last 1} | `
    ForEach-Object {Import-TabDelimited $_}  | `
 Select-Object "Archive Name",@{Name="Displayname";Expression={$_."Archive Name"}},"Billing Account","Total Items","Total Size (MB)","Archive Description"
 
